﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CompactFramework.Utilities.Localization;

namespace TripLOAMobile
{
    public partial class ShowSendMessage : LocalizedForm
    {
        private AnimationControl animation;

        #region Initialization
        /// <summary>
        /// Initializes a new instance of the ShowSendMessage class.
        /// </summary>
        public ShowSendMessage()
        {
            #region creation of the control for the animation
            animation = new AnimationControl();
            int x = this.Width / 2 - animation.Width / 2;
            int y = this.Height / 2 - animation.Height / 2;
            animation.Location = new Point(x, y);
            this.Controls.Add(animation);
            animation.BringToFront();
            #endregion
            InitializeComponent();
            comboBoxBuddyList.DropDownStyle = ComboBoxStyle.DropDown;
            this.commonConstructorCode();
            this.AddMenusToDictionary();
            UpdateResources();
            if (Welcome.formBuffer.GetFormNames().Contains("ShowProfileInfo"))
                menuItem3.Enabled = true;
            else menuItem3.Enabled = false;
        }

        /// <summary>
        /// Initializes a new instance of the ShowSendMessage class.
        /// </summary>
        /// <param name="receiver">The receiver.</param>
        public ShowSendMessage(string receiver)
        {
            #region creation of the control for the animation
            animation = new AnimationControl();
            int x = this.Width / 2 - animation.Width / 2;
            int y = this.Height / 2 - animation.Height / 2;
            animation.Location = new Point(x, y);
            this.Controls.Add(animation);
            animation.BringToFront();
            #endregion
            InitializeComponent();
            this.commonConstructorCode();
            this.AddMenusToDictionary();
            UpdateResources();
            //fldReceiver.Text = receiver;
            comboBoxBuddyList.Text = receiver;
            if (Welcome.formBuffer.GetFormNames().Contains("ShowProfileInfo"))
                menuItem3.Enabled = true;
            else menuItem3.Enabled = false;
        }

        /// <summary>
        /// Initializes a new instance of the ShowSendMessage class.
        /// </summary>
        /// <param name="receiver">The receiver.</param>
        /// <param name="title">The object of the message.</param>
        public ShowSendMessage(string receiver, string title)
        {
            #region creation of the control for the animation
            animation = new AnimationControl();
            int x = this.Width / 2 - animation.Width / 2;
            int y = this.Height / 2 - animation.Height / 2;
            animation.Location = new Point(x, y);
            this.Controls.Add(animation);
            animation.BringToFront();
            #endregion
            InitializeComponent();
            this.commonConstructorCode();
            this.AddMenusToDictionary();
            UpdateResources();
            //fldReceiver.Text = receiver;
            comboBoxBuddyList.Text = receiver;
            fldObject.Text = title;
            if (Welcome.formBuffer.GetFormNames().Contains("ShowProfileInfo"))
                menuItem3.Enabled = true;
            else menuItem3.Enabled = false;
        }

        /// <summary>
        /// Commons the constructor code.
        /// </summary>
        private void commonConstructorCode()
        {
            this.comboBoxBuddyList.DropDownStyle = ComboBoxStyle.DropDown;
            List<WS_SoapLogin.UserInfo> buddyList = Welcome.rrp.GetBuddyList();
            if (buddyList != null)
            {
                foreach (WS_SoapLogin.UserInfo user in buddyList)
                {
                    this.comboBoxBuddyList.Items.Add(user.Username);
                }
                if (this.comboBoxBuddyList.Items.Count > 0)
                {
                    this.comboBoxBuddyList.SelectedIndex = 0;
                }
                }
        }

        /// <summary>
        /// The subclasses of the LocalizedForm should override this method and use this method to
        /// associate all menu items with custom names with the help of AddMenuToDictionary
        /// method.
        /// </summary>
        protected override void AddMenusToDictionary()
        {
            this.AddMenuToDictionary(this.menuLeft, "menuLeft");
            this.AddMenuToDictionary(this.menuRight, "menuRight");
            this.AddMenuToDictionary(this.menuItem1, "menuBack");
            this.AddMenuToDictionary(this.menuItem2, "menuHome");
            this.AddMenuToDictionary(this.menuItem3, "menuProfile");
        }
        #endregion;

        #region Response Handling
        /// <summary>
        /// Handles the send response.
        /// </summary>
        /// <param name="e">The BooleanResponseEventArgs instance containing the event data.</param>
        private void rrp_SendResponse(RemoteResourceProvider.BooleanResponseEventArgs e)
        {
            if ((e.Status == RemoteResourceProvider.okStatus) && e.Succeed)
            {
                MessageBox.Show(ResourceManager.Instance.GetString("SentMessage"));
            }
            else
            {
                MessageBox.Show(ResourceManager.Instance.GetString("NotSentMessage"));
            }
            this.stopAnimation();
            Welcome.formBuffer.Close();
        }
        #endregion

        #region OnPainting
        /// <summary>
        /// Handles the Paint event.
        /// </summary>
        /// <param name="e">A PaintEventArgs that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            //Added to prevent painting of "ok" button
            WinAPI.ShowDoneButton(this.Handle, false);
            base.OnPaint(e);
            using (Graphics g = e.Graphics)
            {
                g.DrawImage(Properties.Resources.circles, 3, AutoScrollPosition.Y + 3);
                Point[] p = new Point[]{
                    new Point(40,AutoScrollPosition.Y + 25), 
                    new Point(195,AutoScrollPosition.Y + 25), 
                    new Point(190,AutoScrollPosition.Y + 30), 
                    new Point(40,AutoScrollPosition.Y + 30)};
                using (Brush b = new SolidBrush(Color.Green))
                    g.FillPolygon(b, p);

                p[0].Y = p[1].Y = AutoScrollPosition.Y + 30;
                p[2].Y = p[3].Y = AutoScrollPosition.Y + 35;
                p[1].X -= 10;
                p[2].X -= 10;
                using (Brush b = new SolidBrush(Color.GreenYellow))
                    g.FillPolygon(b, p);

                using (Brush b = new SolidBrush(Color.FromArgb(48, 138, 13)))
                {
                    Rectangle r = new Rectangle(this.comboBoxBuddyList.Location.X - 3, this.comboBoxBuddyList.Location.Y - 3, this.comboBoxBuddyList.Width + 6, this.comboBoxBuddyList.Height + 6);
                    g.FillRectangle(b, r);
                    r.X = this.fldObject.Location.X - 3; r.Y = this.fldObject.Location.Y - 3; r.Width = this.fldObject.Width + 6; r.Height = this.fldObject.Height + 6;
                    g.FillRectangle(b, r);
                    r.X = this.fldText.Location.X - 3; r.Y = this.fldText.Location.Y - 3; r.Width = this.fldText.Width + 6; r.Height = this.fldText.Height + 6;
                    g.FillRectangle(b, r);
                }
            }
        }
        #endregion;

        #region Event Handling
        /// <summary>
        /// Handles the Click event of the menuRight control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuRight_Click(object sender, EventArgs e)
        {
            WS_SoapMessage.Message message = new WS_SoapMessage.Message();
            message.IsRead = false;
            message.Priority = false;
            message.Sender = Welcome.rrp.GetCurrentUserName();
            message.Receiver = comboBoxBuddyList.Text.Split(';',',');
            message.Title = fldObject.Text;
            message.Text = fldText.Text;
            bool utonto = false;
            foreach (string r in message.Receiver)
                if (sender.Equals(r)) utonto = true;
            if (!utonto)
            {
                Welcome.rrp.SendMessage(this, rrp_SendResponse, message, message.Receiver);
                this.startAnimation();
            }
            else MessageBox.Show(ResourceManager.Instance.GetString("Utonto"));
        }

        #region SipHandling
        private object focused;

        /// <summary>
        /// Handles the GotFocus event of the txtBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void txtBox_GotFocus(object sender, EventArgs e)
        {
            //Shows the INPUT_PANEL
            focused = sender;
            sip.Enabled = true;
        }

        /// <summary>
        /// Handles the LostFocus event of the txtBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void txtBox_LostFocus(object sender, EventArgs e)
        {
            //Hides the INPUT_PANEL
            sip.Enabled = false;
        }

        /// <summary>
        /// Handles the Changed event of the sip control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void sip_Changed(object sender, EventArgs e)
        {
            if (focused == fldText)
                fldText.Height = sip.VisibleDesktop.Height - ((sip.Enabled) ? 0 : 26) - fldText.Top - 3;

        }
        #endregion

        #endregion;

        #region start/stop animation
        delegate void animationDel();
        /// <summary>
        /// Stops the animation.
        /// </summary>
        private void stopAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = true;
            }
            this.menuLeft.Enabled = true;
            this.menuRight.Enabled = true;
            this.animation.StopAnimation();
            this.AutoScroll = true;
        }
        /// <summary>
        /// Starts the animation.
        /// </summary>
        private void startAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = false;
            }
            this.menuLeft.Enabled = false;
            this.menuRight.Enabled = false;
            this.animation.StartAnimation();
            this.AutoScroll = false;
        }
        #endregion

        /// <summary>
        /// Handles the Click event of the menuItem1(back) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem1_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.Close();
        }

        /// <summary>
        /// Handles the Click event of the menuItem2(back to home) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem2_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.Clear();
        }

        /// <summary>
        /// Handles the Click event of the menuItem3(back to...) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem3_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.GoTo("ShowProfileInfo");
        }
    }
}